/**
 * @(#)UserSettingCtrl.java	09/05/2013
 *
 * Copyright (c) 2013 S9,Inc.All rights reserved.
 * Created by 2013-09-05 
 */
package cn.jdy.common.ctrl;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 移动Phone系统过滤器,并控制cookie读写
 *
 * @author XiaoZhenx
 * @description: 单独针对移动phone的系统访问过滤器
 * @date 2013-09-05
 */
public class PhoneSecFilter implements Filter {

	//过滤器初始化访问路径参数
	private static String urls;

	/**
	 * :参数初始化
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		urls = filterConfig.getInitParameter("urls");
	}

	/**
	 * : 系统初始化
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		//获取请求的url地址
		String uri = req.getRequestURI();
		//过滤器器中维护的特定url和s9模块中的jsp页面不进行拦截
		if(!uri.matches(urls) && uri.indexOf("/s9/") != 0) {
			//如果没有登陆，或者请求session超时都返回重新登陆
			Object so = req.getSession().getAttribute("userInfo");
			if(so == null || so.equals("")) {
				PrintWriter out = res.getWriter();
				out.print("<html>");
				out.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
				out.print("<script> ");
				out.print("window.top.location='/phone/member/login.jsp';");
				out.print("</script>");
				out.print("</html>");
				return;
			}
		}
		chain.doFilter(request, response);
	}

	/**
	 * :系统关闭时的资源释放操作
	 */
	public void destroy() {

	}

}
